package project.active;


import project.dao.CommentDAO;
import project.dao.TestProjectDAO;
import project.model.PrProject;
import project.model.PrTestProject;
import model.pojo.Employee;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import approve.action.R;

public class ProjectManagerErrorDetail extends Activity {
	private TextView curNameProject, lbTitleValue;
	//, lbBttActive, lbBttView,Delete;
	private TextView valTitle;
	private Button bttView, bttEdit, bttDelete, bttActive;
	private String lang = "EN", database = "admin";
	private Employee emp = new Employee();
	private PrProject prProject = null;
	private PrTestProject prTestProject = null;
	TextView logout, username;
	Bundle language;

	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}

	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.projectmanagererrordetail);

		language = getIntent().getExtras().getBundle(getLang());

		curNameProject = (TextView) findViewById(R.id.currentProjectNameID);
		valTitle = (TextView) findViewById(R.id.valueTitle);

		Intent intent = this.getIntent();
		prProject = (PrProject) intent.getSerializableExtra("project");
		emp = (Employee) intent.getSerializableExtra("em");
		prTestProject = (PrTestProject) intent
				.getSerializableExtra("errorproject");
		System.out.println("test project error : +++++++"
				+ prTestProject.getStatus());

		curNameProject.setText(prProject.getNameProject());
//		valTitle.setText(prTestProject.getTitle());
		valTitle.setText(prTestProject.getTitleview());

//		bttActive = (Button) findViewById(R.id.idButtonActive);
		

		bttView = (Button) findViewById(R.id.idButtonView);
		bttEdit = (Button) findViewById(R.id.idButtonEdit);
		bttDelete = (Button) findViewById(R.id.idButtonDelete);

		logout = (TextView) findViewById(R.id.logout);
		username = (TextView) findViewById(R.id.username);
		logout.setText(language.getString("Logout"));

		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());

		lbTitleValue = (TextView) findViewById(R.id.idTitleError);
//		lbBttActive = (TextView) findViewById(R.id.idButtonActive);
//		lbBttView = (TextView) findViewById(R.id.idButtonView);
//		lbBttEdit = (TextView) findViewById(R.id.idButtonEdit);
//		lbBttDelete = (TextView) findViewById(R.id.idButtonDelete);

		lbTitleValue.setText(language.getString("title")+ ":");
//		bttActive.setText(language.getString("ac_active"));
		bttView.setText(language.getString("View"));
		bttEdit.setText(language.getString("Edit"));
		bttDelete.setText(language.getString("Delete"));
		
		bttView.setOnClickListener(processButtonView);
		bttEdit.setOnClickListener(processButtonEdit);
		bttDelete.setOnClickListener(new OnClickListener() {
			
							public void onClick(View v) {
								showPopupDelete();
							}
						});
			
//		if (prTestProject.getStatus() == 0 || prTestProject.getStatus() == 1) {
//			bttView.setEnabled(true);
//			bttActive.setEnabled(false);
//			bttView.setOnClickListener(processButtonView);
//
//			// bttEdit = (Button)findViewById(R.id.idButtonEdit);
//			bttEdit.setOnClickListener(processButtonEdit);
//
//			// bttDelete = (Button)findViewById(R.id.idButtonDelete);
//			bttDelete.setOnClickListener(new OnClickListener() {
//
//				public void onClick(View v) {
//					showPopupDelete();
//				}
//			});
//		} else if (prTestProject.getStatus() == 2) {
//			bttView.setEnabled(false);
//			bttActive.setEnabled(true);
//			bttActive.setOnClickListener(processButtonActive);
//			// bttEdit = (Button)findViewById(R.id.idButtonEdit);
//			bttEdit.setOnClickListener(processButtonEdit);
//
//			// bttDelete = (Button)findViewById(R.id.idButtonDelete);
//			bttDelete.setOnClickListener(new OnClickListener() {
//
//				public void onClick(View v) {
//					showPopupDelete();
//				}
//			});
//		}

	}

	private OnClickListener listenerLogout = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i = new Intent(ProjectManagerErrorDetail.this,
					LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};

	protected void showPopupDelete() {
		AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		helpBuilder.setTitle(language.getString("lndelerr"));
		helpBuilder.setMessage(language.getString("deleteerror"));
		helpBuilder.setPositiveButton("Ok",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
							CommentDAO.deleteComment(prTestProject.getIdTest(),
									getDatabase());
							TestProjectDAO.deleteTestProject(
									prTestProject.getIdTest(), getDatabase());
							Intent intent = new Intent(
									ProjectManagerErrorDetail.this,
									ListErrorProjectManager.class);
							intent.putExtras(getIntent().getExtras());
							intent.putExtra("prproject", prProject);
							intent.putExtra("empl", emp);
							// intent.putExtra("errorproject", prTestProject);
							intent.putExtra("database", getDatabase());
							intent.putExtra("lang", lang);
						
							startActivity(intent);
							finish();
					}
				});

		helpBuilder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// Do nothing
					}
				});

		// Remember, create doesn't show the dialog
		AlertDialog helpDialog = helpBuilder.create();
		helpDialog.show();
	}

	private OnClickListener processButtonView = new OnClickListener() {

		public void onClick(View v) {
			Intent intent = new Intent(ProjectManagerErrorDetail.this,
					ProcessButtonViewErrorManagerProject.class);
			intent.putExtras(getIntent().getExtras());
			intent.putExtra("emp", emp);
			intent.putExtra("prproject", prProject);
			intent.putExtra("prprojecttest", prTestProject);
			intent.putExtra("database", getDatabase());
			intent.putExtra("lang", lang);
			
			startActivity(intent);
		}
	};

	private OnClickListener processButtonEdit = new OnClickListener() {

		public void onClick(View v) {
			Intent intent = new Intent(ProjectManagerErrorDetail.this,
					ProcessButtonEditManagerProject.class);
			intent.putExtras(getIntent().getExtras());
			intent.putExtra("emp", emp);
			intent.putExtra("prproject", prProject);
			intent.putExtra("prprojecttest", prTestProject);
			intent.putExtra("database", getDatabase());
			intent.putExtra("lang", lang);
			
			startActivity(intent);
		}
	};

	private OnClickListener processButtonActive = new OnClickListener() {

		public void onClick(View v) {
				TestProjectDAO.activeTestTemp(prTestProject.getIdTest(),
						getDatabase());
				Intent intent = new Intent(ProjectManagerErrorDetail.this,
						ListErrorProjectManager.class);
				intent.putExtras(getIntent().getExtras());
				intent.putExtra("prproject", prProject);
				intent.putExtra("empl", emp);
				intent.putExtra("database", getDatabase());
				intent.putExtra("lang", lang);
				

		}
	};
}
